package com.example.Lezedata.ui.fragment.commonfragment;

import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.example.Lezedata.R;
import com.example.Lezedata.ui.fragment.BaseDiaLogFragment;
import com.example.Lezedata.util.GBTimeManager;

import java.util.Date;

import common.Common;

public class DateSelect extends BaseDiaLogFragment {
    public static DateSelect newInstance() {
        return new DateSelect();
    }

    public interface confirmListener{
        void selectDate(String time);
    }
    public confirmListener listener;

    private TextView confirm;
    private ImageView close;
    private DatePicker datePicker;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.date_select, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        confirm = findViewWithTag("confirm");
        close = findViewWithTag("close");

        Date date = new Date();

        datePicker = findViewWithTag("datePicker");
        datePicker.setMaxDate(date.getTime());

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectFinish();
            }
        });
    }

    void selectFinish(){
        if (listener != null){
            listener.selectDate(String.format("%d-%02d-02d",datePicker.getYear(),datePicker.getMonth(),datePicker.getDayOfMonth()));
            dismiss();
        }
    }
}
